package org.csu.mypetstore.controller;


import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/catalogM")
@SessionAttributes(value = {"account", "order", "cart", "userInfo"})
public class CatelogMController {

      @Autowired
      private CatalogService catalogService;


      @RequestMapping("/gotoAddCategory")
      String GotoAddCategory() {

            return "managerCatalog/addCategory";
      }

      @RequestMapping("/gotoModifyCategory")
      String GotoModifyCategory(String categoryId,Model model) {


            Category category = catalogService.getCategory(categoryId);
            model.addAttribute("choosedCategory",category);
            return "managerCatalog/modifyCategory";
      }


      @RequestMapping("/addCategory")
      String AddCategory(String categoryId, String name, Model model) {

            if (categoryId == null) {
                  model.addAttribute("message", "传入错误");
            } else {
                  Category category = new Category();
                  category.setCategoryId(categoryId);
                  category.setName(name);
                  category.setDescription("");
                  catalogService.insertCategory(category);
            }

            List<Category> categoryList = catalogService.selectCategory("");
            model.addAttribute("categoryList", categoryList);
            return "managerCatalog/allCategory";
      }

      @RequestMapping("/modifyCategory")
      String modifyCategory(String categoryId, String name, Model model) {

            System.out.println(categoryId+" ::" +name);
            if (categoryId == null) {
                  model.addAttribute("message", "传入错误");
            } else {
                  Category category = new Category();
                  category.setCategoryId(categoryId);
                  category.setName(name);
                  catalogService.updateCategory(category);
            }

            List<Category> categoryList = catalogService.selectCategory("");
            model.addAttribute("categoryList", categoryList);
            return "managerCatalog/allCategory";

      }

      @RequestMapping("/deleteCategory")
      String deleteCategory(String categoryId, Model model) {
            if (categoryId == null) {
                  model.addAttribute("message", "传入错误");
            } else {
                  catalogService.deleteCategory(categoryId);
            }

            List<Category> categoryList = catalogService.selectCategory("");
            model.addAttribute("categoryList", categoryList);
            return "managerCatalog/allCategory";

      }


      @RequestMapping("/viewAllCategory")
      String ViewAllCategory( Model model) {

            List<Category> categoryList = catalogService.selectCategory("");

            model.addAttribute("categoryList", categoryList);

            return "managerCatalog/allCategory";

      }

      @RequestMapping("/viewCategory")
      String ViewCategory(String str, Model model) {
            if (str == null) {
                  model.addAttribute("message", "传入错误");
            } else {
                  List<Category> categoryList = catalogService.selectCategory(str);
                  model.addAttribute("categoryList", categoryList);
            }
            return "managerCatalog/allCategory";

      }


      @RequestMapping("/gotoAddProduct")
      String GotoAddProduct(Model model) {

            List<Category> categoryList = catalogService.getCategoryList();
            model.addAttribute("categoryList",categoryList);
            return "managerCatalog/addProduct";
      }

      @RequestMapping("/gotoModifyProduct")
      String GotoModifyProduct(String productId,Model model) {

            System.out.println("modify"+productId);
            Product product = catalogService.getProduct(productId);
            List<Category> categoryList = catalogService.getCategoryList();
            model.addAttribute("product",product);
            model.addAttribute("categoryList",categoryList);

            return "managerCatalog/modifyProduct";
      }


      @RequestMapping("/addProduct")
      String AddProduct(String productId, String categoryId, String name, String description, Model model) {

            if (productId == null) {
                  model.addAttribute("message", "传入错误");
            } else {

                  Product product = new Product();
                  product.setProductId(productId);
                  product.setCategoryId(categoryId);
                  product.setName(name);
                  product.setDescription(description);
                  catalogService.insertProduct(product);

                  List<Product> productList = catalogService.getAllProducts(categoryId);
                  model.addAttribute("productList",productList);
            }

            return "managerCatalog/category";
      }

      @RequestMapping("/modifyProduct")
      String ModifyProduct(String productId, String categoryId, String name, String description, Model model) {

            if (productId == null) {
                  model.addAttribute("message", "传入错误");
            } else {
                  Product product = new Product();
                  product.setProductId(productId);
                  product.setCategoryId(categoryId);
                  product.setName(name);

                  product.setDescription(description);

                  catalogService.updateProduct(product);
            }

            List<Product> productList = catalogService.getAllProducts(categoryId);
            model.addAttribute("productList",productList);
            return "managerCatalog/category";//返回product页面
      }


      @RequestMapping("/deleteProduct")
      String deleteProduct(String productId, Model model) {
            if (productId == null) {
                  model.addAttribute("message", "传入错误");
            } else {
                  Product product = catalogService.getProduct(productId);
                  String categoryId = product.getCategoryId();

                  catalogService.deleteProduct(productId);
                  List<Product> productList = catalogService.getAllProducts(categoryId);
                  model.addAttribute("productList",productList);

            }


            return "managerCatalog/category";//返回product页面

      }


      @RequestMapping("/viewProduct")
      String ViewProduct(String categoryId, Model model) {
            if (categoryId== null) {
                  model.addAttribute("message", "传入错误");
            } else {
                  List<Product> productList = catalogService.getAllProducts(categoryId);
                  model.addAttribute("productList", productList);
            }
            model.addAttribute("categoryId",categoryId);
            return "managerCatalog/category";//返回category页面


      }




      @RequestMapping("/gotoAddItem")
      String GotoAddItem(Model model) {

            List<Product> productList = catalogService.getAllProducts("");
            model.addAttribute("productList",productList);
            return "managerCatalog/addItem";
      }

      @RequestMapping("/gotoModifyItem")
      String GotoModifyItem(String itemId,Model model) {

            Item item = catalogService.getItem(itemId);
            model.addAttribute("item",item);
            List<Product> productList = catalogService.getAllProducts("");
            model.addAttribute("productList",productList);

            return "managerCatalog/modifyItem";

      }



      @RequestMapping("/addItem")
      public String addItem(String itemId, String productId, BigDecimal listPrice, BigDecimal unitCost,
                            int supplierId, String status, String attribute1, String attribute2, String attribute3,
                            String attribute4, String attribute5, Boolean isInStore, Model model) {
            Item item = new Item();
            item.setItemId(itemId);
            item.setProductId(productId);
            item.setListPrice(listPrice);
            item.setUnitCost(unitCost);
            item.setSupplierId(supplierId);
            item.setStatus(status);
            item.setAttribute1(attribute1);
            item.setAttribute2(attribute2);
            item.setAttribute3(attribute3);
            item.setAttribute4(attribute4);
            item.setAttribute5(attribute5);
            item.setInStore(isInStore);

            catalogService.insertItem(item);
            String message = "增加Item成功";
            model.addAttribute("message", message);

            List<Item> itemList = catalogService.getAllItems(productId);
            model.addAttribute("itemList",itemList);


            return "managerCatalog/product";

      }

      @RequestMapping("/modifyItem")
      public String modifyItem(String itemId, String productId, BigDecimal listPrice, BigDecimal unitCost,
                               int supplierId, String status, String attribute1, String attribute2, String attribute3,
                               String attribute4, String attribute5, Boolean isInStore, Model model) {
            Item item = new Item();
            item.setItemId(itemId);
            item.setProductId(productId);
            item.setListPrice(listPrice);
            item.setUnitCost(unitCost);
            item.setSupplierId(supplierId);
            item.setStatus(status);
            item.setAttribute1(attribute1);
            item.setAttribute2(attribute2);
            item.setAttribute3(attribute3);
            item.setAttribute4(attribute4);
            item.setAttribute5(attribute5);
            item.setInStore(isInStore);

            catalogService.updateItem(item);
            String message = "修改Item成功";
            model.addAttribute("message", message);

            List<Item> itemList = catalogService.getAllItems(productId);
            model.addAttribute("itemList",itemList);
            return "managerCatalog/product";
      }


      @RequestMapping("/deleteItem")
      String deleteItem(String itemId, Model model) {
            if (itemId == null) {
                  model.addAttribute("message", "传入错误");
            } else {


                  Item item = catalogService.getItem(itemId);
                  System.out.println(item);
                  System.out.println("****************");
                  String productId = item.getProduct().getProductId();

                  catalogService.deleteItem(itemId);

                  List<Item> itemList = catalogService.getAllItems(productId);
                  model.addAttribute("itemList",itemList);
            }


            return "managerCatalog/product";

      }

      @RequestMapping("/viewItem")
      String ViewItem(String productId, Model model) {

            List<Item> itemList = catalogService.getAllItems(productId);
            System.out.println("hsj");
            model.addAttribute("itemList", itemList);
            model.addAttribute("productId",productId);
            return "managerCatalog/product";

      }

      @RequestMapping("/viewItemC")
      String ViewItemC(String itemId, Model model) {
            System.out.println("传入了asd itemOD"+itemId);
            if (itemId== null) {
                  model.addAttribute("message", "传入错误");
            } else {
                  Item item = catalogService.getItem(itemId);
                  System.out.println("*********************");
                  System.out.println(item.getInStore());
                  model.addAttribute("item",item);
            }
            return "managerCatalog/item";

      }

      @RequestMapping("/confirmShip")
      String ConfirmShip(String itemId ,Model model) {

            catalogService.upConfirmItem(itemId);

            Item item = catalogService.getItem(itemId);
            String productId = item.getProduct().getProductId();
            List<Item> itemList = catalogService.getAllItems(productId);
            model.addAttribute("itemList", itemList);
            model.addAttribute("productId",productId);


            return "managerCatalog/product";

      }

      @RequestMapping("/cancelShip")
      String CancelShip(String itemId ,Model model) {

            catalogService.upCancelItem(itemId);
            System.out.println("CatelogController:367:传入的itemID"+itemId);
            System.out.println("***************************");
            Item item = catalogService.getItem(itemId);
            System.out.println("CatelogController:370:得到的item"+item);
            String productId = item.getProduct().getProductId();
            System.out.println("CatelogController:371:返回的productID"+productId);
            List<Item> itemList = catalogService.getAllItems(productId);
            model.addAttribute("itemList", itemList);
            model.addAttribute("productId",productId);




            return "managerCatalog/product";

      }




}
